001 /* 002 * Copyright 2004 Stephen J. McConnell. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 013 * implied. 014 * 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 019 package net.dpml.metro.data; 020 021 import net.dpml.component.Directive; 022 023 /** 024 * A <code>FeatureDirective</code> declares a context entry that is itself a feature 025 * of the component including the component uri, name, working directory, or temporary 026 * directory. 027 * 028 * @author <a href="http://www.dpml.net">Digital Product Meta Library</a> 029 * @version 1.0.1 030 */ 031 public class FeatureDirective extends AbstractDirective implements Directive 032 { 033 /** 034 * Serial version identifier. 035 */ 036 static final long serialVersionUID = 1L; 037 038 /** 039 * Constant identifier for the component name feature. 040 */ 041 public static final int NAME = 0; 042 043 /** 044 * Constant identifier for the component uri feature. 045 */ 046 public static final int URI = 1; 047 048 /** 049 * Constant identifier for the component working directory feature. 050 */ 051 public static final int WORK = 2; 052 053 /** 054 * Constant identifier for the component temporary directory feature. 055 */ 056 public static final int TEMP = 3; 057 058 /** 059 * Return the feature id givcen a supplied name. 060 * @param value the feature name 061 * @return the feature id 062 */ 063 public static int getFeatureForName( String value ) 064 { 065 if( "name".equals( value ) ) 066 { 067 return NAME; 068 } 069 else if( "uri".equals( value ) ) 070 { 071 return URI; 072 } 073 else if( "work".equals( value ) ) 074 { 075 return WORK; 076 } 077 else if( "temp".equals( value ) ) 078 { 079 return TEMP; 080 } 081 else 082 { 083 final String error = 084 "Feature not recognized [" + value + "]."; 085 throw new IllegalArgumentException( error ); 086 } 087 } 088 089 //-------------------------------------------------------------------------- 090 // state 091 //-------------------------------------------------------------------------- 092 093 private final String m_key; 094 private final int m_feature; 095 096 //-------------------------------------------------------------------------- 097 // constructors 098 //-------------------------------------------------------------------------- 099 100 /** 101 * Create a new feature directive. 102 * @param key the feature name 103 * @param feature the feasture id 104 */ 105 public FeatureDirective( String key, int feature ) 106 { 107 if( null == key ) 108 { 109 throw new NullPointerException( "key" ); 110 } 111 m_key = key; 112 m_feature = feature; 113 } 114 115 //-------------------------------------------------------------------------- 116 // FeatureDirective 117 //-------------------------------------------------------------------------- 118 119 /** 120 * Return the feature key. 121 * @return the feature key 122 */ 123 public String getKey() 124 { 125 return m_key; 126 } 127 128 /** 129 * Return the feature id. 130 * @return the feature id 131 */ 132 public int getFeature() 133 { 134 return m_feature; 135 } 136 137 /** 138 * Test if the supplied object is equal to this object. 139 * @param other the object to compare with this instance 140 * @return TRUE if the supplied object is equal to this object 141 */ 142 public boolean equals( Object other ) 143 { 144 if( null == other ) 145 { 146 return false; 147 } 148 else 149 { 150 FeatureDirective feature = (FeatureDirective) other; 151 if( !m_key.equals( feature.getKey() ) ) 152 { 153 return false; 154 } 155 else 156 { 157 return m_feature == feature.getFeature(); 158 } 159 } 160 } 161 162 /** 163 * Return the hashcode for the instance. 164 * @return the instance hashcode 165 */ 166 public int hashCode() 167 { 168 int hash = m_key.hashCode(); 169 hash ^= m_feature; 170 return hash; 171 } 172 }